package com.ssy.lingxi.report.serviceimpl;

import com.ssy.lingxi.common.model.dto.UserLoginCacheDTO;
import com.ssy.lingxi.common.response.Wrapper;
import com.ssy.lingxi.report.api.enums.OperateDataSourceEnum;
import com.ssy.lingxi.report.api.vo.request.OperateNoticeVO;
import com.ssy.lingxi.report.entity.report.AppBacklogNoticeDO;
import com.ssy.lingxi.report.model.vo.response.AppBacklogNoticeDetailVO;
import com.ssy.lingxi.report.model.vo.response.AppBacklogNoticeVO;
import com.ssy.lingxi.report.repository.report.AppBacklogNoticeRepository;
import com.ssy.lingxi.report.service.IAppBacklogNoticeService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import javax.persistence.criteria.Predicate;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * App待办通知接口实现
 * @author 伍将
 * @date 2021/1/18
 * @version 2.0.0
 */
@Service
public class AppBacklogNoticeServiceImpl implements IAppBacklogNoticeService {

    @Resource
    private AppBacklogNoticeRepository repository;

    /**
     * 更新通知
     * @author 伍将
     * @date 2021/1/18
     * @param operateVO: 操作通知
     **/
    @Override
    public void updateNotice(OperateNoticeVO operateVO) {

        // 如果减少待办数据id大于0，则删除原待办通知
        if (null != operateVO.getReduceDataId() && operateVO.getReduceDataId() > 0) {
            repository.deleteNotice(operateVO.getMemberId(), operateVO.getRoleId(), operateVO.getDataSource(), operateVO.getReduceOperateType(), operateVO.getReduceDataId());
        }

        // 如果新增待办数据id大于0且标题不为空时，则新增待办通知
        if (null != operateVO.getIncreaseDataId() && operateVO.getIncreaseDataId() > 0
                && StringUtils.hasLength(operateVO.getIncreaseTitle())) {
            AppBacklogNoticeDO appBacklogNoticeDO = new AppBacklogNoticeDO();
            appBacklogNoticeDO.setMemberId(operateVO.getMemberId());
            appBacklogNoticeDO.setRoleId(operateVO.getRoleId());
            appBacklogNoticeDO.setDataSource(operateVO.getDataSource());
            appBacklogNoticeDO.setOperateType(operateVO.getIncreaseOperateType());
            appBacklogNoticeDO.setDataId(operateVO.getIncreaseDataId());
            appBacklogNoticeDO.setTitle(operateVO.getIncreaseTitle());
            appBacklogNoticeDO.setCreateTime(System.currentTimeMillis());
            repository.saveAndFlush(appBacklogNoticeDO);
        }
    }

    /**
     * 获取待办通知列表
     * @author 伍将
     * @date 2021/1/18
     * @param user:当前登录用户
     * @param count: 查询数量
     * @return 待办列表
     **/
    @Override
    public Wrapper<AppBacklogNoticeVO> getNoticeList(UserLoginCacheDTO user, Integer count) {
        AppBacklogNoticeVO result = new AppBacklogNoticeVO();

        Specification<AppBacklogNoticeDO> spec =  (root, query, criteriaBuilder) -> {
            List<Predicate> list = new ArrayList<>();
            list.add(criteriaBuilder.equal(root.get("memberId").as(Long.class), user.getMemberId()));
            list.add(criteriaBuilder.equal(root.get("roleId").as(Long.class), user.getMemberRoleId()));
            Predicate[] p = new Predicate[list.size()];
            return criteriaBuilder.and(list.toArray(p));
        };
        Pageable page = PageRequest.of(0, count, Sort.by("createTime").descending());
        Page<AppBacklogNoticeDO> pageResult = repository.findAll(spec, page);
        result.setTotalCount(pageResult.getTotalElements());
        result.setDetailList(pageResult.getContent().stream().map(r -> {
                    AppBacklogNoticeDetailVO detail = new AppBacklogNoticeDetailVO();
                    detail.setTypeName(OperateDataSourceEnum.getItemName(r.getDataSource()));
                    detail.setTitle(r.getTitle());
                    detail.setId(r.getId());
                    detail.setType(r.getDataSource());
                    detail.setDataId(r.getDataId());
                    return detail;
                }).collect(Collectors.toList())
        );

        return Wrapper.success(result);
    }
}
